﻿namespace BadHabit.Factory
{
    using System;
    using System.IO;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Navigation;
    using System.ComponentModel;
    using System.Collections.Generic;
    using System.Text;
    using BadHabit.Factory.Windows.Properties;

    /// <summary>
    /// This is another part of the client.
    /// It uses the old friend NotifyIcon from Windows Form time and emulates real double-clicks
    /// at the icon.
    /// </summary>
    public partial class Client : Application
    {
        // the icon
        private static System.Windows.Forms.NotifyIcon notifyIcon;
        private static System.Drawing.Icon oldIcon = global::BadHabit.Factory.Windows.Properties.Resources.factoryDisconnected; // used at the icon's change

        // used to differ a single from a double click
        private static int clickDelay = 0;
        private static bool isDoubleClick = false;
        private static System.Windows.Forms.Timer clickTimer = new System.Windows.Forms.Timer();

        /// <summary>
        /// Creates the notify icon using the old windows form framework.
        /// We still waiting bill to release this component at WPF.
        /// </summary>
        private static void CreateNotifyIcon()
        {
            notifyIcon = new System.Windows.Forms.NotifyIcon();
            notifyIcon.Icon = global::BadHabit.Factory.Windows.Properties.Resources.factoryDisconnected;
            notifyIcon.Visible = true;

            notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(NotifyIconMouseClick);
            notifyIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(NotifyIconMouseDoubleClick);

            // have to use the Forms version
            System.Windows.Forms.ContextMenuStrip contextMenu = new System.Windows.Forms.ContextMenuStrip { Name = "contextMenu" };
            contextMenu.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
            contextMenu.ShowImageMargin = false;
            System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem { Name = "exitToolStripMenuItem", Text = global::BadHabit.Factory.Windows.Properties.Resources.Exit };
            System.Windows.Forms.ToolStripMenuItem mainWindowToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem { Name = "mainWindowToolStripMenuItem", Text = global::BadHabit.Factory.Windows.Properties.Resources.Go_to_project_cockpit };
            System.Windows.Forms.ToolStripMenuItem moreTasksToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem { Name = "moreTasksToolStripMenuItem", Text = global::BadHabit.Factory.Windows.Properties.Resources.Give_me_more_tasks };
            System.Windows.Forms.ToolStripMenuItem connectTasksToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem { Name = "ConnectToolStripMenuItem", Text = global::BadHabit.Factory.Windows.Properties.Resources.Re_connect };
            System.Windows.Forms.ToolStripMenuItem settingsTasksToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem { Name = "SettingsToolStripMenuItem", Text = global::BadHabit.Factory.Windows.Properties.Resources.Settings };
            System.Windows.Forms.ToolStripMenuItem teamAllocationToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem { Name = "TeamAllocationToolStripMenuItem", Text = global::BadHabit.Factory.Windows.Properties.Resources.Team_aAllocation };

            mainWindowToolStripMenuItem.Click += new EventHandler(MainWindowToolStripMenuItemClick);
            exitToolStripMenuItem.Click += new EventHandler(ExitToolStripMenuItemClick);
            moreTasksToolStripMenuItem.Click += new EventHandler(MoreTasksToolStripMenuItemClick);
            settingsTasksToolStripMenuItem.Click += new EventHandler(SettingsTasksToolStripMenuItemClick);
            connectTasksToolStripMenuItem.Click += new EventHandler(ConnectTasksToolStripMenuItemClick);
            teamAllocationToolStripMenuItem.Click += new EventHandler(TeamAllocationToolStripMenuItemClick);

            // contextMenu
            contextMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { mainWindowToolStripMenuItem, moreTasksToolStripMenuItem, settingsTasksToolStripMenuItem, teamAllocationToolStripMenuItem, connectTasksToolStripMenuItem, new System.Windows.Forms.ToolStripSeparator(), exitToolStripMenuItem });
            notifyIcon.ContextMenuStrip = contextMenu;

            // sets the doubleClick timer
            clickTimer.Interval = 50;
            clickTimer.Tick += new EventHandler(ClickTimerTick);
        }

        private static void TeamAllocationToolStripMenuItemClick(object sender, EventArgs e)
        {
            Views.TeamAllocation w = new Views.TeamAllocation();
            w.Show();
        }

        /// <summary>
        /// Tries to reconnect to the service.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void ConnectTasksToolStripMenuItemClick(object sender, EventArgs e)
        {
            Connect(true);
        }

        /// <summary>
        /// Shows settings form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void SettingsTasksToolStripMenuItemClick(object sender, EventArgs e)
        {
            BadHabit.Factory.Views.Main.ShowSettingsWindow(false);
        }

        /// <summary>
        /// Bind again the tasks to see if there is anything new.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void MoreTasksToolStripMenuItemClick(object sender, EventArgs e)
        {
            TaskSelector.Bind();
        }

        /// <summary>
        /// When the notify icon exit menu item is clicked, shutdown the application.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void ExitToolStripMenuItemClick(object sender, EventArgs e)
        {
            EndApplication();
        }

        /// <summary>
        /// When the notify icon main window menu item is clicked, show the main window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void MainWindowToolStripMenuItemClick(object sender, EventArgs e)
        {
            ShowMainWindow();
        }

        /// <summary>
        /// Starts a timer to differ a single from a double click.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void NotifyIconMouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                return;
            }

            // if not using task track, just open the window
            if (!global::BadHabit.Factory.Windows.Properties.Settings.Default.TaskTrackActive)
            {
                ShowMainWindow();
                return;
            }

            isDoubleClick = false;
            clickTimer.Start();
        }

        /// <summary>
        /// Just set a double click.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void NotifyIconMouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            isDoubleClick = true;
        }

        /// <summary>
        /// Occurs every 50 miliseconds after a click.
        /// When the Double Click time has already past identify what action should be taken:
        /// SlideUp (single click) or ShowMainWindow (double click).
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void ClickTimerTick(object sender, EventArgs e)
        {
            clickDelay += clickTimer.Interval;
            if (clickDelay >= System.Windows.Forms.SystemInformation.DoubleClickTime)
            {
                clickDelay = 0;
                clickTimer.Stop();

                if (isDoubleClick)
                {
                    ShowMainWindow();
                }
                else
                {
                    Client.TaskSelector.SlideUp();
                }
            }
        }

        /// <summary>
        /// Sets the reading icon when reading.
        /// </summary>
        public static bool IsReadingData
        {
            set
            {
                if (value)
                {
                    if (FactoryMainWindow == null)
                    {
                        return;
                    }

                    FactoryMainWindow.IsWaiting = true;
                    oldIcon = notifyIcon.Icon;
                    notifyIcon.Icon = global::BadHabit.Factory.Windows.Properties.Resources.factoryReading;
                }
                else
                {
                    if (FactoryMainWindow == null)
                    {
                        return;
                    }

                    FactoryMainWindow.IsWaiting = false;
                    notifyIcon.Icon = oldIcon;
                }
            }
        }

        /// <summary>
        /// Show the alert with the number of new tasks for the user.
        /// </summary>
        /// <param name="count">The number of tasks</param>
        public static void ShowTaskAlertToolTip(int count)
        {
            StringBuilder msg = new StringBuilder();
            msg.AppendFormat(global::BadHabit.Factory.Windows.Properties.Resources.You_have_N_new_task, count);
            notifyIcon.ShowBalloonTip(2000, "Factory", msg.ToString(), System.Windows.Forms.ToolTipIcon.Info);
        }

        public static string WorkingOnTaskName
        {
            set
            {
                ////notifyIcon.Text = "Working on '" + value + "'";
            }
        }
    }
}